//引入vuex
import Vuex from 'vuex'
import Vue from 'vue'
Vue.use(Vuex)
//引入api接口
import {reqLogin} from '@/api'
import {setToken} from '@/utils/token'



//准备actions——用于相应组件中的动作 dispatch
const actions = {
    //登录操作
    async userinfo({commit},data){
        let result = await reqLogin(data)
        if (result.status == 0) {
            commit('USERINFO',result.token)
            //持久化存储token
            setToken(result.token)
            return '登录成功'
        }else{
            return Promise.reject(new Error('登录失败'))
        }
    },
}
//准备mutations——用于操作数据 commint
const mutations = {
    USERINFO(state,value){
        state.token = value
    },
    LOGINEXIT(state){
        state.token = ''
        localStorage.removeItem('TOKEN')
    }
}
//准备state——用于存储数据
const state = {
    token:localStorage.getItem('TOKEN'),
}

//创建并暴露store
export default ({
    actions,
    mutations,
    state,
})
